<?php
/**
 * CreateTime: 2023/6/30 18:10
 */

namespace App\Http\Controllers\MeSms;

use App\Models\MeLawCase\MeLawCase;
use App\Models\MeLawCase\MeLawCaseLawLetterModel;
use App\Models\SMS\SMSCallbackModel;
use App\Models\SMS\SMSUpstreamModel;
use App\Tools\Log\DabaicaiSMSCallBackLog;
use App\Tools\Log\QxwgSMSCallBackLog;
use App\Tools\Sms\DbaicaiSms;

class MeSMSQxwgCallbackController
{
    ###################### 企信网关
    /**
     * 报错回调
     * @return string
     */
    public function errorCallBack(){
        $data = request()->all();
        QxwgSMSCallBackLog::E(json_encode($data,256));
        $smsCallBackModel = new SMSCallbackModel();
            foreach($data as $key => $value){
                if(isset($value['mobile'])){
                    $res = $smsCallBackModel->add([
                        'msg_id' => $value['mid'],
                        'type_str' => '企信网关',
                        'receive_time' => $value['time'],
                        'status' => $value['stat'],
                        'phone' => $value['mobile']
                    ]);
                }
            }


        return "OK";
    }

    /**
     * 上行回复
     * @return string
     */
    public function upCallBack(){
        $data = request()->all();
        QxwgSMSCallBackLog::i(json_encode($data,256));
        $upm = new SMSUpstreamModel();
        $upm->add([
            'phone' => $data[0]['mobile'],
            'content' => $data[0]['content'],
            'receiveTime' => $data[0]['time'],
            'type_str' => '企信网关'
        ]);
        return "OK";
    }

    ############################################### 大白菜

    public function dbcCallBack(){
        $data = request()->all();
        DabaicaiSMSCallBackLog::E(json_encode($data,256));
        $smsCallBackModel = new SMSCallbackModel();
        $lawCaseModel = new MeLawCase();
        $lawCaseLatter = new MeLawCaseLawLetterModel();
        $msgAndStatusArr = [];
        foreach($data as $key => $value){
            $smsCallBackModel->add([
                'msg_id' => $value['msgId'],
                'type_str' => '大白菜',
                'receive_time' => $value['receiveTime'],
                'status' => $value['status'],
                'phone' => $value['phone']
            ]);
            // 获取所有的msg和status
            $msgAndStatusArr[] = [
              'msg_id' =>$value['msgId'],
              'status' => $value['status']
            ];
        }
        // 通过msg_id 获取所有的
        $msgIds = array_column($msgAndStatusArr,'msg_id');
        $msgIdKeyStatusMap = array_column($msgAndStatusArr,'status','msg_id');
        $statusMap = [
            'DELIVRD' => '发送成功',
            'UT:0006' => '号码黑名单',
            'UT:0026' => '号码黑名单',
            'UT:0016' => '号码超频',
            'UT:0009' => '地区屏蔽',
            'FAILURE' => '靓号短信'
        ];
        $lawCaseLatters = $lawCaseLatter->select(['law_case_id','msg_id'])->whereIn('msg_id',$msgIds)->get();
        if($lawCaseLatters){
            foreach($lawCaseLatters->toArray() as $key => $lawCaseLatter){
                $status = $msgIdKeyStatusMap[$lawCaseLatter['msg_id']];
                $statusInfo = $statusMap[$status] ?? "号码异常".$status;
                if($lawCaseLatters[$key]['status']=='DELIVRD'){
                    $lawCaseModel->where('law_case_id',$lawCaseLatter['law_case_id'])->update([
                        'send_law_latter_status' => 1,
                        'send_law_latter_info' => $statusInfo
                    ]);
                }else{
                    $lawCaseModel->where('law_case_id',$lawCaseLatter['law_case_id'])->update([
                        'send_law_latter_status' => 2,
                        'send_law_latter_info' => $statusInfo
                    ]);
                }
            }
        }
    }

    public function dbcUpCallBack(){
        $data = request()->all();
        DabaicaiSMSCallBackLog::i(json_encode($data,256));
        $smsUp = new SMSUpstreamModel();
        // 当运营商没有返回报错的时候再填充数据
        // 进入数据库
        foreach($data as $value){
            $smsUp->add([
                'phone' => $value['phone'],
                'content' => $value['content'],
                'receiveTime' => $value['receiveTime'],
                'type_str' => "大白菜"
            ]);
        }

    }

}